package com.example.liang.basamovie.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.liang.basamovie.R;
import com.example.liang.basamovie.activity.FilmInfoActivity;
import com.example.liang.basamovie.activity.PlayerInfoActivity;
import com.example.liang.basamovie.bean.FIndFilmResourcesBean;
import com.example.liang.basamovie.bean.FindBean;
import com.example.liang.basamovie.bean.FindFilmCasts;
import com.example.liang.basamovie.utils.ImageLoaderCircleUtils;
import com.example.liang.basamovie.utils.ImageLoaderUtils;
import com.example.liang.basamovie.utils.MyAnimation;

import java.util.List;

/**
 *z最新的适配器
 */
public class MustNewAdapter extends RecyclerView.Adapter<MustNewAdapter.MyViewHolder>{
    List<FindBean> list;
    Context context;

    public MustNewAdapter(List<FindBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.fragment_find_mustnewitem_layout, parent, false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final FindBean findBean = list.get(position);

        FIndFilmResourcesBean filmResourcesBean = findBean.getfIndFilmResourcesBean();
        List<FindFilmCasts> list = filmResourcesBean.getList();
        holder.nickname.setText(findBean.getNickname());
        holder.recommend.setText(findBean.getRecommend());
        holder.praiseCount.setText(findBean.getPraiseCount()+"");

        final FIndFilmResourcesBean fIndFilmResourcesBean = findBean.getfIndFilmResourcesBean();
        holder.filmTitle.setText(fIndFilmResourcesBean.getFilm_title());
        ImageLoaderCircleUtils.getimage(context,findBean.getHeadImgUrl(),holder.headImage);
        ImageLoaderUtils.getimage(context,fIndFilmResourcesBean.getImage_url(),holder.filmImage);
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i <list.size() ; i++) {

            if(i!=list.size()-1){
                buffer.append(list.get(i).getName()+"/");
            }else {
                buffer.append(list.get(i).getName());
            }

        }
        holder.filmCasts.setText("主演："+buffer);
        //设置film点击事件
        holder.Film_relativeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, FilmInfoActivity.class);
                intent.putExtra("id",fIndFilmResourcesBean.getId());
                Log.e("io","---"+fIndFilmResourcesBean.getId());
                context.startActivity(intent);
            }
        });
        //设置标记的点击事件
        holder.SignLinearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO 标记的点击事件
            }
        });
        //设置喜欢图标的点击事件
        final boolean[] isLike = {true};
        holder.find_praiseCountImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //MyAnimation.SetAnimation(holder.find_praiseCountImage);
                if(isLike[0]){
                    holder.find_praiseCountImage.setImageResource(R.drawable.zan_press_new);
                    isLike[0] =false;
                    String string = holder.praiseCount.getText().toString();
                    int newNum = Integer.parseInt(string)+1;
                    holder.praiseCount.setText(newNum+"");

                }else{
                    holder.find_praiseCountImage.setImageResource(R.drawable.zan_new_white);
                    isLike[0] =true;
                    String string = holder.praiseCount.getText().toString();
                    int newNum = Integer.parseInt(string)-1;
                    holder.praiseCount.setText(newNum+"");
                }
                MyAnimation.SetAnimation(holder.find_praiseCountImage);

            }
        });
        //设置点击作者头像的点击跳转事件
        holder.headImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, PlayerInfoActivity.class);
                intent.putExtra("url",findBean.getId());
                //Log.e("pp","----"+findBean.getId());
                context.startActivity(intent);
            }
        });



    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        ImageView headImage,filmImage,find_praiseCountImage;
        TextView nickname,praiseCount,recommend,filmTitle,filmCasts,filmsTime;
        RelativeLayout Film_relativeLayout;
        LinearLayout SignLinearLayout;
        public MyViewHolder(View itemView) {
            super(itemView);
            headImage= (ImageView) itemView.findViewById(R.id.find_headImage);
            filmImage= (ImageView) itemView.findViewById(R.id.find_filmImage);
            find_praiseCountImage= (ImageView) itemView.findViewById(R.id.find_praiseCountImage);
            nickname= (TextView) itemView.findViewById(R.id.find_nickname);
            praiseCount= (TextView) itemView.findViewById(R.id.find_praiseCount);
            recommend= (TextView) itemView.findViewById(R.id.find_recommend);
            filmTitle= (TextView) itemView.findViewById(R.id.find_filmTitle);
            filmCasts= (TextView) itemView.findViewById(R.id.find_filmCasts);
            filmsTime= (TextView) itemView.findViewById(R.id.find_filmsTime);
            Film_relativeLayout= (RelativeLayout) itemView.findViewById(R.id.film_RelativeLayout);
            SignLinearLayout= (LinearLayout) itemView.findViewById(R.id.find_sign);
        }
    }
}
